﻿Imports System.Linq

Namespace Aricie.DNN.Services.Url.FriendlyUrl.SimpleAPI
    Public NotInheritable Class SimpleGroupParamsRewrite
        Inherits GroupParamsRewrite
        Implements ISimpleRewritingGroup

        Public Delegate Function SimpleRewriteDelegate(ByVal CurrentGroup As SimpleGroupParamsRewrite, ByVal objRewriteType As RewriteType, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String), ByVal strategy As FriendlierUrlStrategy) As String

        Protected Friend Property Rewriter As SimpleRewriteDelegate
        Protected Friend Property Consume As Boolean
        Protected Friend Property ApplyBehaviour As Boolean

        Protected Friend Sub New(ByVal GroupName As String, ByVal GroupDescription As String)
            MyBase.GroupName = GroupName
            MyBase.Description = GroupDescription
            MyBase.RewriteType = FriendlyUrl.RewriteType.Any
            Me.Consume = True
            Me.ApplyBehaviour = True
        End Sub

        Protected Friend Function Rewrite(ByVal objRewriteType As RewriteType, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String), ByVal behaviour As FriendlierUrlStrategy) As GroupRewriteResult Implements ISimpleRewritingGroup.Rewrite
            Dim grw As New GroupRewriteResult()
            If Rewriter IsNot Nothing Then
                Try
                    Dim rewriteResult As String = Rewriter(Me, objRewriteType, params, behaviour)
                    If (Consume) Then
                        For Each Parameter As UrlParam In UrlParams.Keys
                            grw.ConsumedParameters(Parameter) = True
                        Next
                    End If
                    'If (ApplyBehaviour) Then
                    '    RewriteResult = behaviour.StringEscaper.EscapeString(RewriteResult)
                    'End If
                    grw.RewriteValue = rewriteResult
                Catch ex As Exception
                    grw.RewriteValue = String.Empty ' on considère que c'est un échec
                    grw.ConsumedParameters.Clear() ' donc on nettoie tout avant de renvoyer le résultat
                End Try
            End If
            Return grw
        End Function

        ''' <summary>
        ''' If the group uses only one parameter, returns the parameter value as integer
        ''' </summary>
        ''' <param name="params">The parameters contained in the url we try to rewrite</param>
        ''' <returns>The single parameter value as integer</returns>
        ''' <remarks>If the group doesn't use exactly one parameter, a InvalidOperationException is thrown</remarks>
        Public Function SingleParameterAsInteger(ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String)) As Integer
            Return Integer.Parse(SingleParameter(params), Globalization.CultureInfo.InvariantCulture)
        End Function

        ''' <summary>
        ''' If the group uses only one parameter, returns the parameter value as string
        ''' </summary>
        ''' <param name="params">The parameters contained in the url we try to rewrite</param>
        ''' <returns>The single parameter value as string</returns>
        ''' <remarks>If the group doesn't use exactly one parameter, a InvalidOperationException is thrown</remarks>
        Public Function SingleParameter(ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String)) As String
            If (UrlParams.Count <> 1) Then
                Throw New InvalidOperationException(String.Format("This group does not contain just one parameter: {0}. Please use GetParam instead", UrlParams.Count))
            End If

            If params.ContainsKey(UrlParams.Keys.First()) Then
                Return params(UrlParams.Keys.First())
            End If
            
            Throw New InvalidOperationException(String.Format("Parameter not found"))
        End Function

        ''' <summary>
        ''' Returns the parameter value as an integer
        ''' </summary>
        ''' <param name="name">The parameter name</param>
        ''' <param name="params">The parameters contained in the url we try to rewrite</param>
        ''' <returns>The parameter value as integer</returns>
        ''' <remarks></remarks>
        Public Function GetParameterAsInteger(ByVal name As String, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String)) As Integer
            Return Integer.Parse(GetParameter(name, params), Globalization.CultureInfo.InvariantCulture)
        End Function

        ''' <summary>
        ''' Returns the parameter value as string
        ''' </summary>
        ''' <param name="name">The parameter name</param>
        ''' <param name="params">The parameters contained in the url we try to rewrite</param>
        ''' <returns>The parameter value as string</returns>
        ''' <remarks></remarks>
        Public Function GetParameter(ByVal name As String, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String)) As String
            Return params(UrlParam.FromSyntax(name))
        End Function

    End Class
End Namespace
